package com.woniuxy.oa.controller;
 
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.woniuxy.oa.entity.Approval;
import com.woniuxy.oa.entity.Employee;
import com.woniuxy.oa.entity.Goods_claim;
import com.woniuxy.oa.entity.Goods_purchase;
import com.woniuxy.oa.entity.Holiday;
import com.woniuxy.oa.entity.PageBean;
import com.woniuxy.oa.entity.Reimbursement;
import com.woniuxy.oa.exception.ApprovalException;
import com.woniuxy.oa.service.ApprovalService;
import com.woniuxy.oa.utils.GoodsRepositoryUtil;

@Controller
//@RequestMapping("/approval")
public class ApprovalController {

	@Autowired
	ApprovalService approvalService;

	public void setApprovalService(ApprovalService approvalService) {
		this.approvalService = approvalService;
	}

	
	@RequestMapping("/test")
	public void test666() {
		System.out.println("test被访问");
	}
	
	
	@RequestMapping("/testTrans")
	public String testTrans() {
		System.out.println("testTrans被访问");
		return "test";
	}
	
	//审批方法
	@Transactional(rollbackFor = { RuntimeException.class, Error.class, Exception.class })
	@ResponseBody
	@RequestMapping("/examineApply")
	public List<String> examine(String flag,Approval approval,HttpServletRequest request){
		List<String> msg = new ArrayList<String>();
		System.out.println("审批方法被访问flag为"+flag+"approval为："+approval);
		try {
			if(flag==null||approval.getA_type()==null||approval.getA_id()==null||!("agree".equals(flag)||"disagree".equals(flag))) {
				msg.add("参数错误");
				throw new ApprovalException();
			}
			//获取当前用户id
			System.out.println("到1了");
			Employee emp = (Employee) request.getSession().getAttribute("user");
			//根据审批号查审批，检查审批人是否为当前用户
			Approval selectApproval = approvalService.getApprovalByAid(approval.getA_id());
//			if(selectApproval.getA_eid()!=emp.getId()) {
//				msg.add("不要乱搞");
//				throw new ApprovalException();
//			}
			
			//检查重复审批
			if(selectApproval.getA_state()!=1) {
				msg.add("请勿重复审批");
				throw new ApprovalException();
			}
			
			System.out.println("到2了");
			//根据flag改变审批状态
			approvalService.examine(approval.getA_id(),flag);
			
			//向审批表写入审批时间
			approvalService.writeExamineTime(approval.getA_id());
			
			//如果类型为申领或申购，且为已通过  调用王胜奔的更新库存的方法
			if(flag.equals("agree")) {
				//申领
				if(approval.getA_type()==4) {
					Goods_claim goodsClaim = approvalService.getGcidByAid(approval.getA_id());
					if(goodsClaim==null) {
						msg.add("申领更新库存失败");
						throw new ApprovalException();
					}
					Integer gc_id = goodsClaim.getGc_id();
					GoodsRepositoryUtil.updateGoodsRepositoryByGc_id(gc_id );
				}
				if(approval.getA_type()==5) {
					Goods_purchase goodPurchase = approvalService.getGpidByAid(approval.getA_id());
					if(goodPurchase==null) {
						msg.add("申购更新库存失败");
						throw new ApprovalException();
					}
					Integer gp_id =  goodPurchase.getGp_id();
					GoodsRepositoryUtil.updateGoodsRepositoryByGp_id(gp_id);
				}
				
			}
			msg.add("审批成功");
			System.out.println("到3了");
			return msg;
		} catch (Exception e) {
			e.printStackTrace();
			return msg;
		}
	}
	
	
	
	
	
	/*
	 * 通用的查询方法
	 * 
	 */
	@ResponseBody
	@RequestMapping("/getApprovalInfo")
	public PageBean getApprovalInfoByConditionAndPage(HttpServletRequest request, HttpServletResponse response,
			String name, Integer act,Approval approval, Holiday holiday,Reimbursement reimbursement) {
		System.out.println("查询方法收到的名字为："+name);
		System.out.println("查询方法收到的act为："+act);
		System.out.println("查询方法收到的holiday为："+holiday);
		System.out.println("查询方法收到的reimbursement为："+reimbursement);
		System.out.println("查询方法收到的approval为："+approval);
		PageBean<Approval> pb = null;
		try {
			System.out.println("查询审批方法被访问");
			//页码
			Integer pageIndex = getPageIndex(request);
			//每页展示数
			Integer pageSize = 10;
			//设置审批状态，没有默认为1 未审批
			approval = setAstate(approval);
			
			//审批类型,有的话检查是否在范围内，没有则设置approval条件 的审批人id为当前id
			/*
			 * 1 加班
			 * 2 请假
			 * 3 报销
			 * 4 申领
			 * 5 申购
			 * 6 公告
			 * 
			 * 
			 * 
			 * */
			//没有审批类型时,设置当前id为审批人id
			//这里先写死用于测试
//			approval.setA_type(3);
			if(act!=null&&act==1) {
				System.out.println("进到审批查询");
				approval.setA_eid(5);
				if(request.getParameter("a_type") == null || "".equals(request.getParameter("a_type"))) {
//					approval.setA_eid(((Employee)request.getSession().getAttribute("user")).getId());
					//先模拟下
					System.out.println("进到审批查询approval为"+approval);
					//在service中进行判断
					pb = approvalService.getApprovalPageBean(pageIndex,pageSize,name,approval,holiday,reimbursement);
					
				}
				else {
					//查询
					approval.setA_type(approval.getA_type());
					System.out.println("进到审批类型查询approval为"+approval);
					pb = approvalService.getApprovalPageBean(pageIndex,pageSize,name,approval,holiday,reimbursement);
				}
			}else {
				//查询
				System.out.println("进到普通查询");
				approval.setA_type(approval.getA_type());
				//读取session中保存的用户信息
				
				approval.setE_id(9);
				pb = approvalService.getApprovalPageBean(pageIndex,pageSize,name,approval,holiday,reimbursement);
			
			}
			
			return pb;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		
	}

	private Approval checkAType(Approval approval, HttpServletRequest request) {
		if(request.getParameter("a_type") == null || "".equals(request.getParameter("a_type"))) {
			approval.setA_eid(((Employee)request.getSession().getAttribute("user")).getId());
		}
		if(approval.getA_type()<1||approval.getA_type()>6) {
			throw new ApprovalException("类型不正确");
		}
		return approval;
	}


	//获取审批状态方法
	private Approval setAstate(Approval approval) {
		System.out.println("审批状态条件为："+approval.getA_state());
		if(approval.getA_state()==null||approval.getA_state()<=0) {
			approval.setA_state(1);
			return approval;
		}
		return approval;
	}

	// 获取页数方法
	private int getPageIndex(HttpServletRequest request) {
		if (request.getParameter("pageIndex") == null || "".equals(request.getParameter("pageIndex"))) {
			return 1;
		}
		return Integer.parseInt(request.getParameter("pageIndex"));
	}

	// 获取类型方法
	private int getPageBeanType(HttpServletRequest request) {
		if (request.getParameter("pageBeanType") == null || "".equals(request.getParameter("pageBeanType"))) {
			return 1;
		}
		return Integer.parseInt(request.getParameter("pageIndex"));
	}

	@RequestMapping("/findApprovalPersons")
	/**
	 * 准备创建一个审批申请时通过点击下拉框访问； 根据当前用户id找到部门； 根据部门找到部门下所有人； 筛选出具有审批权限的人； 返回给ajax；
	 * 
	 * @return
	 */
	@ResponseBody
	public List<Employee> selectApprovalPersonsById(HttpServletRequest request) {
		List<Employee> approvalPersons = null;
		try {
			HttpSession session = request.getSession();
			// 此处应从session域中获取登录成功时保存的用户，并查看其id
			Employee emp = (Employee) session.getAttribute("user");
			

			// 根据当前用户id查找到拥有审批权限的人，整合登录后从域中获取，现在先写死
			approvalPersons = approvalService.getApprovalPersonsById(9);
			// 存到域中，用于创建申请时检查
			session.setAttribute("approvalPersons", approvalPersons);
			return approvalPersons;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}
	
	
	@RequestMapping("/testNull")
	public void testNull(Approval approval) {
		System.out.println("testnull方法"+approval.getA_type());
	} 

}
